syslib.ui.AccordeonItem = {};
syslib.ui.AccordeonItem.Conversor = syslib.object.createClass();
syslib.ui.AccordeonItem.Conversor.extend(syslib.ui.VisualTag);
syslib.ui.AccordeonItem.Conversor.extend({
	__parentTagName : "accordeonmenu",
	
	id : "", 
	accordeon : null,
	element : null, 
	index : null, 
	image : false, 
	selected : false, 

	prepare : function(){
		// get the tag attributes
		var attributes = this.__attributes;

		// set the tag attributes 
		this.id = (__valid(attributes.id)) ? attributes.id : syslib.element.generateID();
		this.accordeon = this.__parent;
		this.selected = false;
		
		// verify icon
		if(__valid(attributes.icon)){
			this.image = true;
		}
		
		// return
		return true;
	},

	execute : function(){
		// element attributes
		var attributes = syslib.object.extend(this.__attributes, {
			"class" : "syslib-accordeon-item-normal",
			"innerName" : "itemContainer"
		});
		
		// json structure
		var elemtJSON = ["div", attributes, 
			["div", {"class" : "syslib-accordeon-item-title", "innerName" : "title"}],
			["div", {"class" : "syslib-accordeon-item-body-hidden", "innerName" : "body"}, 
				this.__innerHTML
			]
		];
		
		// create the element
		var accordeonItem	= this.createDOM(elemtJSON);

		// update innerhtml
		if(this.image === true){ 
			var icon = this.__attributes.icon; 
			this.__refs.title.innerHTML = "<img width='16' height='16' src='"+icon+"' border='0' align='absmiddle' />" + this.__attributes.label;
		} else {
			this.__refs.title.innerHTML = this.__attributes.label;
		}

		// return
		return accordeonItem;
	},

	finalize : function(){
		// set the index
		this.index = this.accordeon.addItem({
			controller : this,
			body : this.__refs.body
		});

		// select
		if(this.__attributes.selected == "true"){
			this.accordeon.selectItem(this.index);
		}

		// set the events
		syslib.event.listen("mouseover", this.__refs.title, function(){
			if(!this.selected){
				this.__over();
			}
		}.applyTo(this));
		syslib.event.listen("mouseout", this.__refs.title, function(){
			if(!this.selected){
				this.__out();
			}
		}.applyTo(this));
		syslib.event.listen("click", this.__refs.title, function(){
			if(!this.selected){
				this.accordeon.selectItem(this.index);
			}
		}.applyTo(this));
	},

	__select : function(){
		// update the style
		this.__refs.body.className = "syslib-accordeon-item-body-normal";
		this.__element.className = "syslib-accordeon-item-selected";
		
		// set as selected
		this.selected = true;
	},

	__unselect : function(){
		// update the style
		this.__refs.body.className = "syslib-accordeon-item-body-hidden";
		this.__element.className = "syslib-accordeon-item-normal";
		
		// set as unselected
		this.selected = false;
	},

	__over : function(){
		this.__element.className = "syslib-accordeon-item-over";
	}, 
	
	__out : function(){
		this.__element.className = "syslib-accordeon-item-normal";
	}
});